package com.uchengwang.ui.cm_ui.me.presenter;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.uchengwang.functions.http.config.BannerConfig;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ThemeConfig;
import com.uchengwang.functions.http.config.UniConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.me.bean.ArticleRecomBean;
import com.uchengwang.ui.cm_ui.me.bean.ArticleRecomListBean;
import com.uchengwang.ui.cm_ui.me.bean.BannerListBean;
import com.uchengwang.ui.cm_ui.me.bean.UniExamineBean;
import com.uchengwang.ui.cm_ui.me.bean.UniExamineListBean;
import com.uchengwang.ui.cm_ui.me.bean.UniFeedbackBean;
import com.uchengwang.ui.cm_ui.me.bean.UniFeedbackListBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.BannerBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.ThemeBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.ThemeListBean;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/8/21.
 */

public class HttpUniManager {

    private static HttpUniManager manager;
    private HttpUniManager(){}

    public static HttpUniManager getManager() {
        if (manager ==null){
            manager = new HttpUniManager();
        }
        return manager;
    }

    /**
     * 获取自己推荐的banner
     * */
    public void getBanners(final Activity activity, int page, int perPage, final UniBannerListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);

        HttpManager.newInstance(activity).query(UniConfig.Recomment.method_get, UniConfig.Recomment.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);

                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    BannerListBean bean = JsonUtils.getManager().stringToBean(data, BannerListBean.class);
                    List<BannerBean> list = bean.getData();
                    listener.onResult(status, msg,bean.getTotalPage(), list);

                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }


    /**
     * uni推荐上banner
     * */
    public void toBanner(final Activity activity, BannerBean bannerBean, String recommendMsg){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.BANNER_COVER, bannerBean.getBannerCover());
        map.put(BaseConfig.BANNER_TITLE, bannerBean.getBannerTitle());
        map.put(BaseConfig.BANNER_URL, bannerBean.getBannerUrl());
        map.put(BaseConfig.PUB_ID, bannerBean.getPubId());
        map.put(BaseConfig.RECOMMEND_MSG, recommendMsg);
        map.put(BaseConfig.RECOMMEND_UNI_UID, UCommonUtil.getUtil().getId());
        map.put(BaseConfig.UID, bannerBean.getUid());
        if (bannerBean.getUniId() >0){
            map.put(BaseConfig.UNI_ID, bannerBean.getUniId());
        }

        HttpManager.newInstance(activity).query(UniConfig.Recomment.method, UniConfig.Recomment.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().showToast(activity, "推荐失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();

                if (status ==0){
                    UCommonUtil.getUtil().showToast(activity, "推荐成功");
                }else {
                    UCommonUtil.getUtil().showToast(activity, "推荐失败");
                }
            }
        });



    }

    /**
     * uni获取审核数据
     * */
    public void getExamines(Activity activity, int page, int perPage, final UniExamaneListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);
        HttpManager.newInstance(activity).query(UniConfig.publish.method_get, UniConfig.publish.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);

                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    UniExamineListBean bean = JsonUtils.getManager().stringToBean(data, UniExamineListBean.class);
                    List<UniExamineBean> list = bean.getData();
                    listener.onResult(status, msg,bean.getTotalPage(), list);

                }else {
                    listener.onResult(status, msg, 0, null);
                }

            }
        });

    }



    /**
     * Uni提交审核
     *@param examineImgs 图片地址
     * @param illegalReason 举报原因
     * @param pubId
     * @param pubType
     * */
    public void postExamine(final Activity activity, String examineImgs, String illegalReason, long pubId, int pubType){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UNI_UID, UCommonUtil.getUtil().getId());
        if (!TextUtils.isEmpty(examineImgs)){
            map.put(BaseConfig.EXAMINE_IMGS, examineImgs);
        }
        if (!TextUtils.isEmpty(illegalReason)){
            map.put(BaseConfig.ILLEGAL_REASON, illegalReason);
        }
        map.put(BaseConfig.PUB_ID, pubId);
        map.put(BaseConfig.PUB_TYPE, pubType);
        HttpManager.newInstance(activity).query(UniConfig.publish.method, UniConfig.publish.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().showToast(activity, "提交失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();

                if (status ==0){
                    UCommonUtil.getUtil().showToast(activity, "提交成功");
                    activity.finish();
                }else {
                    UCommonUtil.getUtil().showToast(activity, "提交失败");
                }

            }
        });
    }

    /**
     *Uni提交反馈
     * */
    public void postFeedback(final Activity activity, String description, String imgs){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UNI_UID, UCommonUtil.getUtil().getId());
        if (!TextUtils.isEmpty(description)){
            map.put(BaseConfig.DESCRIPTION, description);
        }
        if (!TextUtils.isEmpty(imgs)){
            map.put(BaseConfig.IMGS, imgs);
        }
        if (UChengApplication.uniId >0){
            map.put(BaseConfig.UNI_ID, UChengApplication.uniId);
        }
        if (!TextUtils.isEmpty(UChengApplication.mySchoolName)){
            map.put(BaseConfig.UNI_NAME, UChengApplication.mySchoolName);
        }

        HttpManager.newInstance(activity).query(UniConfig.feedback.method, UniConfig.feedback.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();

                if (status ==0){
                    UCommonUtil.getUtil().showToast(activity, "提交成功");
                    activity.finish();
                }else {
                    UCommonUtil.getUtil().showToast(activity, "提交失败");
                }

            }
        });
    }

    /**
     * uni获取反馈
     * */
    public void getFeedback(Activity activity, int page, int perPage, final UniFeedbackListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);
        HttpManager.newInstance(activity).query(UniConfig.feedback.method_get, UniConfig.feedback.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);

                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    UniFeedbackListBean bean = JsonUtils.getManager().stringToBean(data, UniFeedbackListBean.class);
                    List<UniFeedbackBean> list = bean.getData();
                    listener.onResult(status, msg, bean.getTotalPage(), list);

                }else {
                    listener.onResult(status, msg, 0, null);
                }


            }
        });
    }


    /**
     * uni获取推荐的热门帖子
     * */
    public void getHotArticles(Activity activity, int page, int perPage, final UniHotArticleListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);
        HttpManager.newInstance(activity).query(UniConfig.HotArticle.method_get, UniConfig.HotArticle.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);

                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    ArticleRecomListBean bean = JsonUtils.getManager().stringToBean(data, ArticleRecomListBean.class);
                    List<ArticleRecomBean> list = bean.getData();
                    listener.onResult(status, msg,bean.getTotalPage(), list);

                }else {
                    listener.onResult(status, msg, 0, null);
                }

            }
        });

    }

    /**
     * uni推荐热门帖子
     * */
    public void postHotArticle(final Activity activity, ArticleRecomBean bean){
        if (bean == null){
            return;
        }
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UNI_UID, UCommonUtil.getUtil().getId());
        map.put(BaseConfig.PUB_ID, bean.getPubId());
        map.put(BaseConfig.PUB_UID, bean.getPubUid());

        if (bean.getUniId() >0){
            map.put(BaseConfig.UNI_ID, bean.getUniId());

            if (!TextUtils.isEmpty(bean.getUniName())){
                map.put(BaseConfig.UNI_NAME, bean.getUniName());
            }
        }

        if (!TextUtils.isEmpty(bean.getRecommendReason())){
            map.put(BaseConfig.RECOMMEND_REASON, bean.getRecommendReason());
        }
        if (!TextUtils.isEmpty(bean.getRecommendMsg())){
            map.put(BaseConfig.RECOMMEND_MSG, bean.getRecommendMsg());
        }
        if (!TextUtils.isEmpty(bean.getRecommendImgs())){
            map.put(BaseConfig.RECOMMEND_IMGS, bean.getRecommendImgs());
        }

        HttpManager.newInstance(activity).query(UniConfig.HotArticle.method_post, UniConfig.HotArticle.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().showToast(activity, "推荐失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                if (status ==0){
                    UCommonUtil.getUtil().showToast(activity, "推荐成功");
                }else {
                    UCommonUtil.getUtil().showToast(activity, "推荐失败");
                }

            }
        });

    }

    /**
     * UNi发布的头条 下架处理
     * @param id banner id
     * */
    public void soldOutBanner(final Activity activity, final long id, final OnResultListener listener){
        DialogUtils.getUtils().showDialogSingle(activity, "确定下架此banner?", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.getUtils().cancel();

                LoadingUtils.getUtils().loading(activity);
                MapRequest map = new MapRequest();
                map.put(BaseConfig.ID, id);
                HttpManager.newInstance(activity).query(BannerConfig.Banner.method_delete, BannerConfig.Banner.URL, map, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        LoadingUtils.getUtils().hide();
                        listener.onResult(status, msg, 0, null);
                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        LoadingUtils.getUtils().hide();
                        listener.onResult(status, msg, 0, null);
                    }
                });
            }
        });


    }

    /**
     * 推荐上U城主页热门 U学 U赛 U行 公益倡议 校吧帖子
     * */
    public void recommentHot(final Activity activity, long pubId, String recommentMsg, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ID, pubId);
//        map.put(BaseConfig.RECOMMEND_MSG, recommentMsg);
        HttpManager.newInstance(activity).query(UniConfig.ReleaseHot.method_recom, UniConfig.ReleaseHot.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                if (listener != null){
                    listener.onResult(status, msg, 0, null);
                }
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                if (listener != null){
                    listener.onResult(status, msg, 0, null);
                }
                if (status ==0){
                    UCommonUtil.getUtil().showToast(activity, "推荐成功");
                }else {
                    UCommonUtil.getUtil().showToast(activity, "推荐失败");
                }
            }
        });

    }


    /**
     * 发布一个热门信息 U学 U赛 U行 公益倡议  校吧帖子
     * */
    public void releaseHot(Activity activity, MapRequest map, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        HttpManager.newInstance(activity).query(UniConfig.ReleaseHot.method, UniConfig.ReleaseHot.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });

    }

    /**
     * 获取uni发布的话题
     * */
    public void getUniTheme(Activity activity, int page, int perPage, final OnHttpResultListener<ThemeBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 10);
        HttpManager.newInstance(activity).query(ThemeConfig.UniTheme.method, ThemeConfig.UniTheme.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status,msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    ThemeListBean bean = JsonUtils.getManager().stringToBean(data, ThemeListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status,msg, 0, null);
                }
            }
        });
    }

    /**
     * uni关闭自己发布的话题
     * */
    public void closeTheme(Activity activity, long themeId, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.TOPIC_ID, themeId);
        HttpManager.newInstance(activity).query(ThemeConfig.Theme.method_delete, ThemeConfig.Theme.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }




    public interface UniExamaneListener{
        void onResult(int stutas, String msg, int totalPages, List<UniExamineBean> list);
    }

    public interface UniBannerListener{
        void onResult(int stutas, String msg, int totalPage, List<BannerBean> list);
    }

    public interface UniHotArticleListener{
        void onResult(int stutas, String msg, int totalPage, List<ArticleRecomBean> list);
    }

    public interface UniFeedbackListener{
        void onResult(int stutas, String msg, int totalPage, List<UniFeedbackBean> list);
    }


}
